const Koa = require('koa2');
const router = require('./router')
const {host, port} = require('./config');
const core = require('koa2-cors')
const bodyParse = require("koa-bodyparser")
// 引入
const path = require('path')
const static = require('koa-static')



const app = new Koa();
//解决跨域问题
app.use(core({
    origin:function(ctx){
        if(ctx.url==="/manage/upload"){
            return "*"
        }
    }
}))
// 获取post请求体参数
app.use(bodyParse())
// 获取静态资源文件夹
app.use(static(path.join(__dirname, '/assets')));
app.use(static(path.join(__dirname, '/router/manage/upload')));
app.use(router.routes(), router.allowedMethods())

app.use(async (ctx, next) => {
    await next();
    if(parseInt(ctx.status) === 404){
        ctx.response.redirect("/404")
    }
})

app.use(router.routes(), router.allowedMethods());

app.listen(port, () => {
    console.log(`Server is running at ${host}:${port}`);
})